import { AppDataSource } from "./data-source"
import { Department } from "./entity/Department"
import { Employee } from "./entity/Employee";

//一对多映射
AppDataSource.initialize().then(async () => {

    //设置 department 的 employees 后 save , 关联的 employee 就会自动保存(然而要设置很多其他的装饰器)
    const D = AppDataSource.manager.getRepository(Department);
  
    // await D.save({ name:'技术部',employees: [
    //     { name:'张三' },
    //     { name:'李四' },
    //     { name:'王五' },
    // ] });

  
    const deps = await D.find({
        relations: {
            employees: true
        }
    });

    console.log(...deps.map(item=>item.employees));

    /*    
    不设置那些乱七八糟的OnetoMany和ManytoOne

    const E = AppDataSource.manager.getRepository(Employee);
    const D = AppDataSource.manager.getRepository(Department);

    let d = await D.save({ name:'技术部' });

    E.save([
        { name:'张三',department:d },
        { name:'李四',department:d },
        { name:'王五',department:d },
    ]) */


}).catch(error => console.log(error))
